﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataLayer;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;

namespace OrderDocument
{
    public partial class frmListe : XtraForm
    {
        public frmListe()
        {
            InitializeComponent();
        }

        public enum ListeTipleri : byte
        {
            CariKodu = 0,
            CariAdi = 1,
            StokKodu = 2,
            StokAdi = 3,
            Barkod = 4,
            EvrakListesi = 5
        }

        public ListeTipleri ListeTipi { get; set; }
        public string Filtre { get; set; }
        public List<string[]> Values { get; set; }

        void ShowList()
        {
            dbListe liste = new dbListe();
            switch (ListeTipi)
            {
                case ListeTipleri.CariKodu:
                    this.Text = "Cari Listesi";
                    gridControl1.DataSource = liste.CariListeKod(this.Filtre);
                    break;
                case ListeTipleri.CariAdi:
                    this.Text = "Cari Listesi";
                    gridControl1.DataSource = liste.CariListeAd(this.Filtre);
                    break;
                case ListeTipleri.StokKodu:
                    this.Text = "Ürün Listesi";
                    gridControl1.DataSource = liste.StokListeKod(this.Filtre, true);
                    foreach (GridColumn col in gridView1.Columns)
                    {
                        col.OptionsColumn.AllowEdit = false;
                    }
                    gridView1.Columns["Miktar"].OptionsColumn.AllowEdit = true;
                    gridView1.Columns["Miktar"].AppearanceCell.BackColor = Color.Red;
                    gridView1.FocusedColumn = gridView1.Columns["Miktar"];
                    break;
                case ListeTipleri.StokAdi:
                    this.Text = "Ürün Listesi";
                    gridControl1.DataSource = liste.StokListeAd(this.Filtre, true);
                    foreach (GridColumn col in gridView1.Columns)
                    {
                        col.OptionsColumn.AllowEdit = false;
                    }
                    gridView1.Columns["Miktar"].OptionsColumn.AllowEdit = true;
                    gridView1.Columns["Miktar"].AppearanceCell.BackColor = Color.Red;
                    gridView1.FocusedColumn = gridView1.Columns["Miktar"];
                    break;
                case ListeTipleri.Barkod:
                    this.Text = "Ürün Listesi";
                    gridControl1.DataSource = liste.StokListeBarkod(this.Filtre, true);
                    foreach (GridColumn col in gridView1.Columns)
                    {
                        col.OptionsColumn.AllowEdit = false;
                    }
                    gridView1.Columns["Miktar"].OptionsColumn.AllowEdit = true;
                    break;
                case ListeTipleri.EvrakListesi:
                    this.Text = "Sipariş Listesi";
                    gridControl1.DataSource = liste.EvrakListesi(this.Filtre);
                    foreach (GridColumn col in gridView1.Columns)
                    {
                        col.OptionsColumn.AllowEdit = false;
                    }
                    gridView1.Columns["Evrak Seri"].MinWidth = 100;
                    gridView1.Columns["Sipariş Tutarı"].MinWidth = 150;
                    break;
                default:
                    break;
            }
            gridView1.BestFitColumns();
            gridView1.Focus();
        }

        private void frmListe_Load(object sender, EventArgs e)
        {
            this.ShowList();
            if (ListeTipi == ListeTipleri.EvrakListesi)
            {
                gridView1.Columns["Sipariş Tutarı"].SummaryItem.SetSummary(DevExpress.Data.SummaryItemType.Sum, "Sipariş Toplam : {0}");
                gridView1.Columns["Evrak Seri"].SummaryItem.SetSummary(DevExpress.Data.SummaryItemType.Count, "Evrak Sayısı : {0}");
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
            {
                List<string[]> values = new List<string[]>();
                List<string> val = new List<string>();
                if (ListeTipi == ListeTipleri.StokAdi || ListeTipi == ListeTipleri.StokKodu || ListeTipi == ListeTipleri.Barkod)
                {
                    updateGrid();
                    for (int i = 0; i < gridView1.RowCount; i++)
                    {
                        if (Convert.ToDecimal(gridView1.GetRowCellDisplayText(i, gridView1.Columns["Miktar"])) > 0)
                        {
                            foreach (DevExpress.XtraGrid.Columns.GridColumn col in this.gridView1.Columns)
                                val.Add(gridView1.GetRowCellValue(i, col).ToString());
                            values.Add(val.ToArray());
                            val.Clear();
                        }
                    }
                }
                else
                {
                    foreach (int item in gridView1.GetSelectedRows())
                    {
                        foreach (DevExpress.XtraGrid.Columns.GridColumn col in this.gridView1.Columns)
                            val.Add(gridView1.GetRowCellValue(item, col).ToString());
                        values.Add(val.ToArray());
                        val.Clear();
                    }
                }

                this.Values = values;
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void updateGrid()
        {
            gridView1.FocusedRowHandle += 1;
            gridView1.FocusedRowHandle -= 1;

            if (gridView1.FocusedRowHandle > -1)
                if (Convert.ToDecimal(gridView1.GetRowCellDisplayText(gridView1.FocusedRowHandle, gridView1.Columns["Miktar"])) == 0)
                {
                    decimal miktar = 0;
                    for (int i = 0; i < gridView1.RowCount; i++)
                    {
                        miktar += Convert.ToDecimal(gridView1.GetRowCellValue(i, gridView1.Columns["Miktar"]));
                    }
                    if (miktar == 0)
                    {
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Miktar"], 1);
                    }
                }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (gridView1.FocusedRowHandle > -1)
                {
                    List<string[]> values = new List<string[]>();
                    List<string> val = new List<string>();
                    if (ListeTipi == ListeTipleri.StokAdi || ListeTipi == ListeTipleri.StokKodu || ListeTipi == ListeTipleri.Barkod)
                    {
                        updateGrid();
                        for (int i = 0; i < gridView1.RowCount; i++)
                        {
                            if (Convert.ToDecimal(gridView1.GetRowCellDisplayText(i, gridView1.Columns["Miktar"])) > 0)
                            {
                                foreach (DevExpress.XtraGrid.Columns.GridColumn col in this.gridView1.Columns)
                                    val.Add(gridView1.GetRowCellValue(i, col).ToString());
                                values.Add(val.ToArray());
                                val.Clear();
                            }
                        }
                    }
                    else
                    {
                        foreach (int item in gridView1.GetSelectedRows())
                        {
                            foreach (DevExpress.XtraGrid.Columns.GridColumn col in this.gridView1.Columns)
                                val.Add(gridView1.GetRowCellValue(item, col).ToString());
                            values.Add(val.ToArray());
                            val.Clear();
                        }
                    }

                    this.Values = values;
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            else if (e.KeyCode == Keys.Escape)
            {
                DialogResult = DialogResult.Abort;
                this.Close();
            }
        }

        private void frmListe_Activated(object sender, EventArgs e)
        {
            gridView1.Focus();
        }
    }
}
